package com.demo.morning;

import java.io.*;

/**
 * @ClassName 对象流
 * @Description TODO
 * @Author tianqinglong
 * @Date 9:31 2022/7/8
 * @Version 1.0
 **/
class Role implements Serializable {
    //无需实现方法 只是在jvm上打上标记
    private String name;
    private int hp;
    private int level;

    public Role(String name, int hp, int level) {
        this.name = name;
        this.hp = hp;
        this.level = level;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getHp() {
        return hp;
    }

    public void setHp(int hp) {
        this.hp = hp;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }
    public void beatEnemy(){
        System.out.println(this.name+"打怪兽");
    }

    @Override
    public String toString() {
        return "Role{" +
                "name='" + name + '\'' +
                ", hp=" + hp +
                ", level=" + level +
                '}';
    }
}
public class 对象流 {
    public static void main(String[] args) throws Exception {
        讲对象写入文件();
        读取对象();
    }

    private static void 读取对象() throws IOException, ClassNotFoundException {
        ObjectInputStream ois=new ObjectInputStream(new FileInputStream("day05//1.dat"));
        //不断地读取文件，直到文件末尾

        try {
            while (true){
                Object obj = ois.readObject();
                System.out.println(obj);
                if (obj instanceof Role){
                    ((Role) obj).beatEnemy();
                }else if(obj instanceof Map){
                    ((Map) obj).showScene();
                }

            }
        }catch (EOFException eofException){
            System.out.println("文件读取结束");
        }
        //ois.close();
        //System.out.println("读取完毕");
    }

    private static void 讲对象写入文件() throws Exception {
        //创建一个对象流写入二进制文件
        ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("day05//1.dat"));
        //创建3个对象
        Role role1 = new Role("李逍遥", 50, 50);
        Role role2 = new Role("重楼", 80, 80);
        Role role3 = new Role("邪剑仙", 9000, 100);

        Map map = new Map(10, 20, "闲乐岛");
        //写入对象流
        oos.writeObject(role1);
        oos.writeObject(role2);
        oos.writeObject(role3);
        oos.writeObject(map);
        oos.close();
        System.out.println("写入成功");
        System.out.println("测试git提交");

    }
}
class Map implements Serializable{
    private int x;
    private int y;
    private String scene;
    public void showScene(){
        System.out.println(this.scene+"出现风景");
    }
    @Override
    public String toString() {
        return "Map{" +
                "x=" + x +
                ", y=" + y +
                ", scene='" + scene + '\'' +
                '}';
    }

    public Map(int x, int y, String scene) {
        this.x = x;
        this.y = y;
        this.scene = scene;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public String getScene() {
        return scene;
    }

    public void setScene(String scene) {
        this.scene = scene;
    }
}